package com.fcgzs.order.controller;

import com.fcgzs.entity.Result;
import com.fcgzs.order.group.Cart;
import com.fcgzs.order.service.CartService;
import com.fcgzs.utils.TokenDecode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @Auther: ljj
 * @Description:
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;

    @RequestMapping("/findCartList")
    public List<Cart> findCartList() {
        //获取当前登录人信息
        Map<String, String> userInfo = tokenDecode.getUserInfo();

        String userId = userInfo.get("username");


        return cartService.findCartListFromRedis(userId);
    }


    @RequestMapping("/addGoodsToCartList")
    public Result addGoodsToCartList(Long itemId, Integer num) {
        //默认设置登录人
        //String userId = "ujiuye";
        //获取当前登录人信息
        Map<String, String> userInfo = tokenDecode.getUserInfo();

        String userId = userInfo.get("username");

        //1.查询原有购物车列表
        List<Cart> cartList = this.findCartList();
        //2.添加购物车
        cartList = cartService.addGoodsToCartList(cartList, itemId, num);

        //3.重新将新的购物车列表放入缓存
        cartService.saveCartListToRedis(cartList, userId);

        return new Result();

    }

}
